//
//  WmcGeneralSearchView.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import UIKit

class WmcGeneralSearchView: WmcBaseView {
    override func makeUI() {
        addSubview(searchBgView)
        searchBgView.addSubview(textfield)
        searchBgView.addSubview(searchIconImageView)
        addSubview(searchBtn)

        searchBgView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
            make.height.equalTo(48)
        }

        searchIconImageView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }

        textfield.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(38)
            make.right.equalTo(-12)
        }

        searchBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
    }

    override func bindViewAction() {
        /// 绑定自定义扩展，clear btn 是否显示
        textfield.rx.text.map(customTransform(text:)).bind(to: textfield.rx.viewMode).disposed(by: rx.disposeBag)
        textfield.rx.text.bind { [weak self] text in
            guard let weakSelf = self, let txt = text else { return }
            if txt.count > 0 {
                weakSelf.searchBgView.snp.remakeConstraints { make in
                    make.top.left.bottom.equalToSuperview()
                    make.height.equalTo(48)
                    make.right.equalTo(-42)
                }
                weakSelf.textfield.snp.updateConstraints { make in
                    make.left.equalTo(12)
                }
                weakSelf.searchIconImageView.isHidden = true
            } else {
                weakSelf.searchBgView.snp.remakeConstraints { make in
                    make.top.left.right.bottom.equalToSuperview()
                    make.height.equalTo(48)
                }
                weakSelf.textfield.snp.updateConstraints { make in
                    make.left.equalTo(38)
                }
                weakSelf.searchIconImageView.isHidden = false
            }
            weakSelf.searchBtn.isHidden = (txt.count > 0) ? false : true
        }.disposed(by: rx.disposeBag)
    }

    lazy var searchBgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.wmc_gray_F7F8FA
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()

    lazy var searchIconImageView: UIImageView = {
        let img = "search".wmc_image.yp.set(tintColor: UIColor.wmc_grayH11_9D9DB3)
        let view = UIImageView(image: img)
        return view
    }()

    lazy var textfield: UITextField = {
        let view = UITextField()
        view.placeholder = "请输入团队号"
        view.modifyClearButtonWithImage(image: UIImage(named: "input_clear_icon"))
        view.rightViewMode = .unlessEditing
        view.textColor = .wmc_grayH11_9D9DB3
        view.font = 16.wmc_font
        return view
    }()

    lazy var searchBtn: WmcNormalButton = {
        let view = WmcNormalButton()
        view.font = 16.wmc_font
        view.textColor = UIColor.wmc_blue_5290FD
        view.title = "搜索"
        view.textAlignment = .center
        view.layer.cornerRadius = 4
        view.clipsToBounds = true
        return view
    }()
}

extension WmcGeneralSearchView {
    /// 为了不让语句过长，闭包写成函数形式
    /// - Parameter text: text description
    /// - Returns: description
    func customTransform(text: String?) -> UITextField.ViewMode {
        return (text ?? "").count > 0 ? UITextField.ViewMode.whileEditing : UITextField.ViewMode.never
    }
}
